package com.activeandroid;

import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.os.Build;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;

public class Application extends android.app.Application {
	private DatabaseManager mDatabaseManager;
	private Set<ActiveRecordBase<?>> mEntities;
	private boolean mShowTrialToast;
	
    private static Application mInstance = null;
    public boolean m_bKeyRight = true;
    private BMapManager mBMapManager = null;

    public static final String strKey = "cDIecePqVXTMQFPYot4eSsW0";

	public void onCreate() {
		super.onCreate();
		this.mEntities = new HashSet();
		
		mInstance = this;
		this.mDatabaseManager = new DatabaseManager(this);
		initEngineManager(this);
	}
	
	public void initEngineManager(Context context) {
        if (mBMapManager == null) {
            mBMapManager = new BMapManager(context);
        }

        if (!mBMapManager.init(strKey,new MKGeneralListener() {

            @Override
            public void onGetNetworkState(int iError) {
                if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
                    Toast.makeText(Application.getInstance().getApplicationContext(), "您的网络出错啦！",
                        Toast.LENGTH_LONG).show();
                }
                else if (iError == MKEvent.ERROR_NETWORK_DATA) {
                    Toast.makeText(Application.getInstance().getApplicationContext(), "输入正确的检索条件！",
                            Toast.LENGTH_LONG).show();
                }
                // ...
            }

            @Override
            public void onGetPermissionState(int iError) {
            	//非零值表示key验证未通过
                if (iError != 0) {
                    //授权Key错误：
                    Toast.makeText(Application.getInstance().getApplicationContext(), 
                            "请在 Application.java文件输入正确的授权Key,并检查您的网络连接是否正常！error: "+iError, Toast.LENGTH_LONG).show();
                    Application.getInstance().m_bKeyRight = false;
                }
                else{
                	Application.getInstance().m_bKeyRight = true;
                	Toast.makeText(Application.getInstance().getApplicationContext(), 
                            "key认证成功", Toast.LENGTH_LONG).show();
                }
            }
        
        })) {
            Toast.makeText(Application.getInstance().getApplicationContext(), 
                    "BMapManager  初始化错误!", Toast.LENGTH_LONG).show();
        }
	}
	
	public BMapManager getMapManager() {
		return mBMapManager;
	}
	
	public static Application getInstance() {
		return mInstance;
	}
	
	public void onTerminate() {
		if (this.mDatabaseManager != null) {
			this.mDatabaseManager.closeDB();
		}

		this.mShowTrialToast = false;

		super.onTerminate();
	}

	final DatabaseManager getDatabaseManager() {
		return this.mDatabaseManager;
	}

	final void addEntity(ActiveRecordBase<?> entity) {
		this.mEntities.add(entity);
	}

	final void addEntities(Set<ActiveRecordBase<?>> entities) {
		this.mEntities.addAll(entities);
	}

	final void removeEntity(ActiveRecordBase<?> entity) {
		this.mEntities.remove(entity);
	}

	final ActiveRecordBase<?> getEntity(
			Class<? extends ActiveRecordBase<?>> entityType, long id) {
		for (ActiveRecordBase<?> entity : this.mEntities) {
			if ((entity.getClass() == entityType)
					&& (entity.getId().longValue() == id)) {
				return entity;
			}
		}

		return null;
	}

	private boolean isEmulator() {
		return (Build.MODEL.equals("sdk"))
				|| (Build.MODEL.equals("google_sdk"));
	}
}
